package redBag;

import java.util.Random;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {

        ArrayBlockingQueue<Integer> redBagQueue = new ArrayBlockingQueue<>(10);
        for (int i = 0; i < 10; i++) {
            redBagQueue.add(i);
        }
        redBagQueue.forEach(System.out::println);
        System.out.println(redBagQueue.stream().mapToInt((x)->x).sum());
        System.out.println(redBagQueue.size());
        RedPackage redPackage = new RedPackage();
        redPackage.remainSize  = 30;
        redPackage.remainMoney = 500;
        double sum = 0;
        for (int i = 0; i < 30; i++) {
            double money = getRandomMoney(redPackage);
            System.out.println(money);
            sum+=money;
        }
        System.out.println(sum);
    }

    public static int getRandomMoney(RedPackage _redPackage) {
        // remainSize 剩余的红包数量
        // remainMoney 剩余的钱
        if (_redPackage.remainSize == 1) {
            _redPackage.remainSize--;
            return (int) Math.round(_redPackage.remainMoney * 100) / 100;
        }
        Random r     = new Random();
        int min   = 1; //
        //红包的最大额度是2*平均值 也就是在均值以上的概率是50 以下的概率是50 保证了公平性
        int max   = (int) (_redPackage.remainMoney / _redPackage.remainSize * 2);
        int money = (int) (r.nextDouble() * max);
        money = money <= min ? 1: money;
        money = (int) (Math.floor(money * 100) / 100);
        _redPackage.remainSize--;
        _redPackage.remainMoney -= money;
        return money;
    }
}
